import { ref, watch } from "vue";

class LayoutNodeRegistry {
  private nodeIds = ref(new Set<string>());
  private static instance: LayoutNodeRegistry;
  private listenerCallback: () => void = () => {};
  private constructor() {
    watch(
      this.nodeIds,
      (newValue) => {
        console.log("watched nodeIds changed", Array.from(newValue) ,Array.from(newValue).length);
        this.listenerCallback();
      },
      { deep: true }
    );
  }
  static getInstance(): LayoutNodeRegistry {
    if (!LayoutNodeRegistry.instance) {
      LayoutNodeRegistry.instance = new LayoutNodeRegistry();
    }
    return LayoutNodeRegistry.instance;
  }
  add(id: string) {
    this.nodeIds.value.add(id);
  }

  remove(id: string) {
    this.nodeIds.value.delete(id);
  }

  has(id: string) {
    return this.nodeIds.value.has(id);
  }

  clear() {
    this.nodeIds.value.clear();
  }

  getAll() {
    return Array.from(this.nodeIds.value);
  }

  size() {
    return this.nodeIds.value.size;
  }

  setListenerCallback(callback: () => void) {
    this.listenerCallback = callback;
  }
}

export const nodeRegistry = LayoutNodeRegistry.getInstance();
